package com.crpc.runner;

import com.crpc.config.CrpcProperties;
import com.crpc.config.RegisterProperties;
import com.crpc.core.CrpcBootStrap;
import com.crpc.core.compress.CompressorFactory;
import com.crpc.core.compress.CompressorWrapper;
import com.crpc.core.discovery.RegistryConfig;
import com.crpc.core.loadBalance.LoadBalanceFactory;
import com.crpc.core.loadBalance.LoadBalanceWrapper;
import com.crpc.core.serialize.SerializerFactory;
import com.crpc.core.serialize.SerializerWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;

@Slf4j
@Component
public class RpcManagerRunner {
    @Autowired
    RegisterProperties registerProperties;

    @Autowired
    CrpcProperties crpcProperties;

    @PostConstruct
    @Order(0)
    public com.crpc.core.config.Configuration configuration() {

        com.crpc.core.config.Configuration configuration = CrpcBootStrap.getInstance().getConfiguration();
        SerializerWrapper serializerWrapper = SerializerFactory.getSerializer(crpcProperties.getSerializerType().getCode());
        configuration.setSerializer(serializerWrapper.getSerializer());
        CompressorWrapper compressorWrapper = CompressorFactory.getCompressor(crpcProperties.getCompressorType().getCode());
        configuration.setCompressor(compressorWrapper.getCompressor());
        LoadBalanceWrapper loadBalanceWrapper = LoadBalanceFactory.getLoadBalance(crpcProperties.getLoadBalancerType().getCode());
        configuration.setLoadBalance(loadBalanceWrapper.getLoadBalance());
        configuration.setPort(crpcProperties.getNettyPort());
        configuration.setApplicationName(crpcProperties.getCrpcName());
        //zookeeper://127.0.0.1:21810
        configuration.setRegistryConfig(new RegistryConfig(registerProperties.getRegistryType().getType() + "://" + registerProperties.getHost() + ":" + registerProperties.getPort()));
        return configuration;
    }

    @PostConstruct
    @Order(2)
    public void test() {
        File directory = new File(".");
        System.out.println("=============================>" + directory.getAbsolutePath());
    }
}